/***********************************
*
* author:huanglei
* mail:code.huanglei@gmail.com
* version:0.3
*
***********************************/

(function(window, undefined){
    
    var 
    
    REG_ID = /^#[\w-]+$/,
    
    Dou = function(selector){
        return new Dou.fn.init(selector);
    };
    
    Dou.fn = Dou.prototype = {
        
        version: '0.3',
        
        init: function(selector){
            
            var match, elem, ret, doc;
        
            if ( selector === undefined) {
                return this;
            }
            
            if (typeof selector === 'String'){
                match = quickExpr.exec( selector );
            
            }
            
            
            return this;
        },
        
        query: function(selector){
            
        },
        
        /*css*/
        css: function(key,value){
            if (value === undefined ){
            
            }
        },
        
        /*document*/
        html: function(html){
            if (html === undefined ){
            
            }
        },
        
        
        /*tools*/
        each: function(arr,fun){
            try{
                for(var i =0 ;i<arr.length ; ++i){
                    try{
                        fun(arr[i],i);    
                    }catch(e){}
                }
            }catch(e){}
        },
        
        makeArray: function(arr,){
        
        },
        
        trim: function(str){
            
        },
        
        /*log*/
        log: function(message){
        
        },
        
        /*error*/
        error: function(message){
            throw message;
        },
        
        now: function() {
		    return ( new Date() ).getTime();
	    },
        
        parseXML: function(data) {
            var xml, tmp;
            try {
                if ( window.DOMParser ) { // Standard
                    tmp = new DOMParser();
                    xml = tmp.parseFromString( data , "text/xml" );
                } else { // IE
                    xml = new ActiveXObject( "Microsoft.XMLDOM" );
                    xml.async = "false";
                    xml.loadXML( data );
                }
            } catch( e ) {
                xml = undefined;
            }
            if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) {
                Dou.error( "Invalid XML: " + data );
            }
            return xml;
        },
        
        parseJSON: function( data ) {
            var
            rvalidchars = /^[\],:{}\s]*$/,
	        rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
	        rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
	        rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
        
            if ( typeof data !== "string" || !data ) {
                return null;
            }
    
            data = Dou.trim( data );
    
            if ( window.JSON && window.JSON.parse ) {
                return window.JSON.parse( data );
            }
    
            // Make sure the incoming data is actual JSON
            // Logic borrowed from http://json.org/json2.js
            if ( rvalidchars.test( data.replace( rvalidescape, "@" )
                .replace( rvalidtokens, "]" )
                .replace( rvalidbraces, "")) ) {
                return ( new Function( "return " + data ) )();
            }
            Dou.error( "Invalid JSON: " + data );
        },

        
        /* ajax */
        httpGet: function(url,fnCallback,rtype){
            var type = rtype?rtype:'xml';
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if (xhr.readyState == 4){
                    if (type === 'xml'){
                        fnCallback(xhr.responseXML);
                    } else if (type === 'json'){
                        fnCallback(JSON.parse(xhr.responseText));
                    } else {
                        fnCallback(xhr.responseText);
                    }
                }
            }; 
            xhr.open('GET',url,true);
            xhr.send();
        }
    };
    
    window.DOU = window.$ = Dou;

})(window);